package cn.webrx;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class List0 {
    public static void main(String[] args) {
        //01 建立ArrayList
        List<String> list1 = new ArrayList<>(5);
        list1.add("java");
        list1.add(0, "php");
//        System.out.println(list1);
//
//        List<String> list2 = new ArrayList<>(List.of("php", "java"));
//        //list2.add("javascript");
//        list2.set(0, "javascript");
//        list2.add(0, "php");
//        //list2.clear();
//        System.out.println(list2.remove("php"));
//        list2.removeAll(List.of("javascript", "java"));
//        System.out.println(list2);
//
//        Integer[] nums = {10, 20, 30};
//        List<Integer> list3 = new ArrayList<>(Arrays.asList(nums));
//        list3.clear();
//
//
//        //将List<String> 转换为 String[]
//        List<String> list4 = List.of("aa","bb","cc");
//        System.out.println(list4);
//
//        String[] sarr = new String[list4.size()];
//        list4.toArray(sarr);
//        System.out.println(sarr);
//        System.out.println(Arrays.toString(sarr));


        //增加元素
        System.out.println(list1);
        list1.add("javasciprt");
        list1.add(0,"mysql");
        list1.addAll(0,List.of("html","vue","vuejs","css","vue"));

        //修改删除元素
        list1.set(0,"html5");
        String temp = list1.get(0);

        list1.set(0,list1.get(list1.size()-1));
        list1.set(list1.size()-1,temp);

        list1.remove("html5");
        //Collection List.of() 返回 List是继承了Collection
        list1.removeAll(List.of("vue","vuejs"));
        //判断元素
        System.out.println(list1.contains("java"));
        System.out.println(list1.indexOf("java"));
        System.out.println(list1.lastIndexOf("java"));
        System.out.println(list1.containsAll(List.of("c","java")));
        System.out.println(list1);

        List<String> list = List.of("jpg","png","gif");
        String fn = "24242424sasf.exe";
        if(list.contains(fn.substring(fn.lastIndexOf(".")+1))){
            System.out.println("服务器允许上传此文件");
        }else{
            System.out.printf("%s 文件，禁止上传",fn);
        }

        //其它操作
        for(String s : list1){
            System.out.println(s);
        }
        System.out.println("-".repeat(15));
        for (int i=0;i<list1.size();i++) {
            System.out.println(list1.get(i));
            System.out.println(list1.get(list1.size()-1-i));
        }
        System.out.println("-".repeat(15));

        //list1.forEach(System.out::println);

        list1.forEach(e->{
            if(e.contains("java")) {
                System.out.println(e);
            }
        });




    }
}
